
const urlParams = (url,params={}) => {
  let paramsUrl = ''
  for (const objKey in params) {
    let p = ''
    if (paramsUrl){
      p = '&'
    } else{
      p = '?'
    }
    /*
    * 重新拼接参数地址，?A&B
    * */
    paramsUrl+=`${p}${objKey}=${params[objKey]}`
  }
  return `${url}${paramsUrl}`
}
const request = ({url,method,data,params,...options}) => {
  if (!options.hideLoading) {
    uni.showLoading({
      title: '加载中...',
      mask: true
    });
  }
  return new Promise((resolve, reject)=>{
    const ip = process.uniEnv.commonPMS20
    uni.request({
      url: ip + urlParams(url, params),
      method,
      dataType: 'json',
      timeout: 10000 * 60,
      header: {
        "sysToken": uni.getStorageSync("sysToken"),
        ...options.header
        //
      },
      data,
      success: (res) => {
        uni.hideLoading();
        if (+res.data.code === 200) {
          resolve(res.data)
        } else {
          uni.showModal({
            title: "温馨提示",
            content: res.data.message || "请求异常",
            showCancel: false,
            confirmColor: '#eb4b4b',
          });
          reject(res.data)
        }
      },
      fail: err => {
        uni.hideLoading();
        reject(err)
      }
    });
  })
}
export const getApi = (url,options)=>{
  return request({ url, ...options, method:'GET' })
}
export const postApi = (url,options) => {
  return request({ url, ...options, method:'POST' })
}
